//
//  MTMapPack.m
//  GoMap
//
//  Created by jsheriff on 2/9/11.
//  Copyright 2011 The MITRE Corporation. All rights reserved.
//

#import "MTMapPack.h"
#import <sqlite3.h>

@implementation MTMapPack

@synthesize fileName;
@synthesize displayName;
@synthesize upperLeftExtent;
@synthesize lowerRightExtent;
@synthesize minZoom;
@synthesize maxZoom;
@synthesize description;
@synthesize layers;
@synthesize thumbnailImage;
@synthesize imageFormat;

-(UIImage *)anyMapImageFromDatabase:(sqlite3 *)database {
	UIImage *image = nil;
	NSString *query = [[NSString alloc] initWithFormat:@"select IMAGE from TILES"];
	sqlite3_stmt *statement;
	if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
		// Get the image from the first row returned
		if (sqlite3_step(statement) == SQLITE_ROW) {
			NSData *imageData = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 0) length:sqlite3_column_bytes(statement, 0)];
			image = [[[UIImage alloc] initWithData:imageData] autorelease];
			[imageData release];
		}
	}
	[query release];
	return image;
}

-(void)loadFromDatabasePath:(NSString *)path {
	if (databaseLock == nil) {
		databaseLock = [[NSLock alloc] init];
	}
	[databaseLock lock];
	sqlite3 *database;
	if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
		NSString *query = @"select name, value from preferences";
		sqlite3_stmt *statement;
		int resultPrepare = sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil);
		if (resultPrepare == SQLITE_OK) {
			double upperLeftLatitude;
			double upperLeftLongitude;
			double lowerRightLatitude;
			double lowerRightLongitude;
			
			while (sqlite3_step(statement) == SQLITE_ROW) {
				const char* name = (const char*) sqlite3_column_text(statement, 0);
				if (strcmp(name, "map.coverage.topLeft.latitude") == 0) {
					upperLeftLatitude = sqlite3_column_double(statement, 1);
				}
				else if (strcmp(name, "map.coverage.topLeft.longitude") == 0) {
					upperLeftLongitude = sqlite3_column_double(statement, 1);
				}
				else if (strcmp(name, "map.coverage.bottomRight.latitude") == 0) {
					lowerRightLatitude = sqlite3_column_double(statement, 1);
				}
				else if (strcmp(name, "map.coverage.bottomRight.longitude") == 0) {
					lowerRightLongitude = sqlite3_column_double(statement, 1);
				}
				else if (strcmp(name, "map.minZoom") == 0) {
					minZoom = sqlite3_column_int(statement, 1);
				}
				else if (strcmp(name, "map.maxZoom") == 0) {
					maxZoom = sqlite3_column_int(statement, 1);
				}
				else if (strcmp(name, "map.displayName") == 0) {
					self.displayName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
				}
				else if (strcmp(name, "map.description") == 0) {
					self.description = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
				}
				else if (strcmp(name, "map.layers") == 0) {
					self.layers = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
				}
				else if (strcmp(name, "map.imageFormat") == 0) {
					self.imageFormat = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
				}
				else if (strcmp(name, "map.thumbnailImage") == 0) {
					NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 1) length:sqlite3_column_bytes(statement, 1)];
					if(data == nil) {
						NSLog(@"No thumbnail found.");
					}
					else {
						thumbnailImage = [UIImage imageWithData:data];
					}
					[data release];
				}
				
			}
			
			upperLeftExtent.latitude = upperLeftLatitude;
			upperLeftExtent.longitude = upperLeftLongitude;
			
			lowerRightExtent.latitude = lowerRightLatitude;
			lowerRightExtent.longitude = lowerRightLongitude;
			
						
		}
		else {
			//
			NSLog(@"Unable to prepare statement");
		}
		if (thumbnailImage == nil) {
			self.thumbnailImage = [self anyMapImageFromDatabase:database];
		}
		sqlite3_close(database);
	}

	[databaseLock unlock];	
}

-(id)initWithPath:(NSString *)path
{
    if (self = [super init])
    {
		self.fileName = path;
		self.displayName = [path lastPathComponent];
		[self loadFromDatabasePath:path];
    }
    return self;
}

-(id)init
{
    return [self initWithPath:nil];
}

- (void)dealloc {
	[fileName release];
	[displayName release];
	[description release];
	[layers release];
	[thumbnailImage release];
	[imageFormat release];
	
	[databaseLock release];

    [super dealloc];
}

@end
